/*******************************************************************
 *
 * Copyright (C), 2021-2022, LS, All rights reserved.
 *
 * Author      : 
 * Create Time : 2021-09-22
 * Version     : v0.1
 * Description : 
 *******************************************************************/
 
/*----------------------------Head file----------------------------*/
#include "pwrmgr.h"
#include "hal_button.h"
#include "bsp_hw_config.h"

#define HAL_BTN_LOG_EN      0

#if HAL_BTN_LOG_EN
#include "log.h"
#define HAL_BTN_LOG         LOG
#else
#define HAL_BTN_LOG(...)
#endif

/*----------------------------macro file---------------------------*/

/*----------------------------type define--------------------------*/

/*----------------------------var define---------------------------*/
static hal_btn_evt_cb_t btn_evt_cb = NULL;

/*-------------------------func declaration------------------------*/

/*-----------------------------------------------------------------*/

static void btn_pin_int_handler(GPIO_Pin_e pin, IO_Wakeup_Pol_e type)
{
    if(NEGEDGE == type) { //Press
        if(btn_evt_cb) {
            btn_evt_cb();
        }
    }
}


bool hal_button_press_state_get(void)
{
    if(false == hal_gpio_read(BSP_BUTTON_PIN_NUM))
        return true;
    else
        return false;
}


int hal_button_init(hal_btn_evt_cb_t cb)
{
    int ret = PPlus_SUCCESS;

    ret += hal_gpio_pin_init(BSP_BUTTON_PIN_NUM, GPIO_INPUT);
    hal_gpio_pull_set(BSP_BUTTON_PIN_NUM, GPIO_PULL_UP);
    ret += hal_gpioin_register(BSP_BUTTON_PIN_NUM, NULL, btn_pin_int_handler);

    btn_evt_cb = cb;

    return ret;
}
